package com.example.sqlitetest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    MyHelper myHelper;
    EditText editId,editName,editPrice;
    private Button btnCreate,btnInsert,btnQuery,btnUpdate,btnDelete;
    private TextView tvQueryData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        initView();
    }

    private void initView() {
        myHelper=new MyHelper(this);
        editId=(EditText) findViewById(R.id.editId);
        editName=(EditText) findViewById(R.id.editName);
        editPrice=(EditText) findViewById(R.id.editPrice);
        btnCreate=(Button) findViewById(R.id.btnCreate);
        btnInsert=(Button) findViewById(R.id.btnInsert);
        btnQuery=(Button)findViewById(R.id.btnQuery);
        btnUpdate=(Button) findViewById(R.id.btnUpdate);
        btnDelete=(Button)findViewById(R.id.btnDelete);
        btnCreate.setOnClickListener(this);
        btnInsert.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        tvQueryData=(TextView) findViewById(R.id.tvQueryData);
    }

    @Override
    public void onClick(View view) {
        int id=Integer.parseInt(editId.getText().toString());
        String name=editName.getText().toString();
        String price=editPrice.getText().toString();
        switch(view.getId()){
            case R.id.btnCreate:
                createDatabase();
                break;
            case R.id.btnInsert:
                long n=insert(name,price);
                if(n>0){
                    Toast.makeText(this,"插入了1条记录,id="+n, Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(this,"插入记录失败！", Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.btnQuery:
                query(id);
                break;
            case R.id.btnUpdate:
               int m= update(name,price);
               if(m>0){
                   Toast.makeText(this,"修改了"+m+"条记录",Toast.LENGTH_LONG).show();
               }else{
                   Toast.makeText(this,"修改记录失败！",Toast.LENGTH_LONG).show();
               }
                break;
            case R.id.btnDelete:
                int l=delete(id);
                if(l>0){
                    Toast.makeText(this,"删除了"+l+"条记录",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(this,"删除记录失败！",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
    public void createDatabase(){
        myHelper=new MyHelper(this);
        //当第一次调用getWritableDatabase()或getReadableDatabase()方法时会创建数据库，并执行onCreate()方法
        SQLiteDatabase db=myHelper.getWritableDatabase();
        String databaseName=myHelper.getDatabaseName();
        Toast.makeText(this,"数据库名："+databaseName,Toast.LENGTH_LONG).show();
    }
    public long insert(String name,String price){
        SQLiteDatabase db=myHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("name",name);
        values.put("price",price);
        long number=db.insert("information",null,values);
        db.close();
        return number;
    }
    public void query(int n){
        SQLiteDatabase db=myHelper.getReadableDatabase();
        Cursor cursor=db.query("information",null,"id=?",new String[]{n+""},null,null,null);
        if(cursor.getCount()!=0){
            while(cursor.moveToNext()){
//                int idIndex=cursor.getColumnIndex("id");
//                int nameIndex=cursor.getColumnIndex("name");
//                int priceIndex=cursor.getColumnIndex("price");
                String id=cursor.getString((int)cursor.getColumnIndex("id"));
                String name=cursor.getString((int)cursor.getColumnIndex("name"));
                String price=cursor.getString((int)cursor.getColumnIndex("price"));
                tvQueryData.setText("id:"+id+"\nname:"+name+"\nprice:"+price);
            }
        }else{
            tvQueryData.setText("未查到指定的记录（id="+n+")");
        }
        cursor.close();
        db.close();
    }
    public int update(String name,String price){
        SQLiteDatabase db=myHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("price",price);
        int number=db.update("information",values,"name=?",new String[]{name});
        db.close();
        return number;
    }
    public int delete(long id){
        SQLiteDatabase db=myHelper.getWritableDatabase();
        int number =db.delete("information","id=?",new String[]{id+""});
        db.close();
        return number;
    }
}
